//待操作文章列表

var UserAddnumberVm = new Vue({
  el: '#user-center-add-number',
  data: {
    addNumber: 0,
    artIndexDict: new Map(),
  },
})

var artIndexDict = UserAddnumberVm.artIndexDict

function checkartIndexDict () {
  if (artIndexDict.size === 0) {
    layer.msg('当前未选中任何文章,无法操作')
    return false
  }
  return true
}

//编辑文章按钮
var userInfoEditEle = document.getElementById('edit-button')
var userInfoAddEle = document.getElementById('add-button')
userInfoEditEle.addEventListener('click', function () {
  if (artIndexDict.size === 0) {
    layer.msg('未选中任何文章无法编辑')
    return
  }

  if (artIndexDict.size > 1) {
    layer.msg('每次只能编辑一篇文章')
    return
  }

  let edit_id = artIndexDict.keys().next().value

  let md = ($('#art-index-' + edit_id).attr('md') === 'False') ? false : true
  if (md) {
    window.location.href = `/edit/${edit_id}?md=true`
    // layer.msg("markdown文章不允许编辑");
    return
  }

  window.location.href = '/edit/' + edit_id
})
userInfoAddEle.addEventListener('click', function () {
  layer.open({
    type: 1,
    shade: false,
    title: '选择编辑器', //不显示标题
    content: `<div style="background-color: #2F4056;text-align: center">
        <a href="/edit/0"><div style="color:#ffffff;display: inline-block;width: 150px;line-height: 8;border-right: 1px solid #5FB878">通用编辑器</div></a>
           <a href="/edit/0?md=true">
           <div style="color:#ffffff;display: inline-block;width: 150px;line-height: 8;border-left: 1px solid #5FB878">MarkDown</div>
        </a>
           </div>`,
  })
})

function get_titles () {
  let artTitles = ''
  for (let value of artIndexDict.values()) {
    artTitles += '<span class="layui-badge-dot layui-bg-black"></span>《' + value + '》<br/>'
  }
  return artTitles
}

//删除文章按钮 / 弹窗
function deleteArt () {
  if (!checkartIndexDict()) {
    return
  }
  let artTitle = get_titles()
  var msg_warning = `
            <h3 style="color:#FFB800">警告！您将要删除以下文章，删除后将不能挽回</h3>${artTitle}
            `

  layer.confirm(msg_warning, {
    btn: ['确认', '取消'] //按钮
    , icon: 7
  }, function () {
    console.log('进入确认删除的方法')
    for (let key of artIndexDict.keys()) {
      deleteArticle(key, false)
    }
  }, function () {
    layer.close()
  })
}

//添加待操作文章
function addArticeId (index, title) {
  let indexId = document.getElementById('art-index-' + index)
  if (indexId.checked) {
    artIndexDict.set(index, title)
    UserAddnumberVm.$forceUpdate()
  } else {
    artIndexDict.delete(index)
    UserAddnumberVm.$forceUpdate()
  }
}

//推送百度
function pushArticle (article_id, author_name) {

  axios.post('/author_article/' + author_name, { 'push': article_id }, {
    headers: {
      'X-CSRFToken': CsrfToken()
    }
  }).then(function (response) {
    let remain = response.data.remain
    console.log(remain)
    if (response.data.success > 0) {
      layer.msg(`推送${article_id.length}篇文章成功,当天剩余次数:${remain}`)
    } else {
      layer.msg('百度PUSH未设置或推送失败,联系管理员')
    }
  }).catch(function (error) {
    console.log(JSON.stringify(error))
  })

}

pushArt = function (username) {
  if (!checkartIndexDict()) {
    return
  }
  let artTitle = get_titles()

  let msg_warning = `
            <h4 style="color:#FFB800">以下文章将被推送到搜索引擎，具体效果需要根据实际情况处理，目前只支持百度主动推送</h4>${artTitle}
        `
  layer.confirm(msg_warning, {
    btn: ['确认', '取消'] //按钮
    , icon: 7
  }, function () {
    console.log('进入推送的方法')
    let keys = []
    let keysMap = artIndexDict.keys()
    while (true) {
      let next = keysMap.next()
      console.log(next)
      if (next.value) {
        keys.push(next.value)
        continue
      }
      break
    }
    pushArticle(keys, username)
  }, function () {
    layer.close()
  })
}

layui.use('element', function () {
  let element = layui.element;
  var layid = location.hash.replace(/^#userCenter=/, '')
  element.tabChange('userCenter', layid)
  element.on('tab(userCenter)', function (elem) {
    location.hash = 'userCenter=' + this.getAttribute('lay-id')
  })
})